/* eslint-disable no-param-reassign */
import React from 'react';
import { runApp } from 'ice';
import cookie from 'react-cookies';
import Loader from '@/components/Loader';
import LocaleProvider from '@/components/LocaleProvider';
import { getLocale } from '@/utils/locale';

const locale = getLocale();
const appConfig = {
  app: {
    rootId: 'ice-container',
    addProvider: ({ children }) => <LocaleProvider locale={locale}>{children}</LocaleProvider>,
  },
  router: {
    type: 'browser',
    fallback: <Loader />,
  },
  request: {
    interceptors: {
      request: {
        onConfig: (config) => {
          if (process.env.NODE_ENV === 'production') {
            // eslint-disable-next-line @iceworks/best-practices/no-http-url
            config.baseURL = 'http://127.0.0.1:8080';
            config.url = config.url.replace(/^\/api\//, '/renren-fast/');
          }
          if (config.headers.token === false) {
            delete config.headers.token;
          } else if (config.headers.token === undefined && cookie.load('token')) {
            config.headers.token = cookie.load('token');
          }
          return config;
        },
      },
    },
  },
};
runApp(appConfig);
